console.log('----')

function addDel(){
    $('.classDel').on('click',function(){
        this_tr =$(this).parent().parent().parent()
        this_formGroup = $(this_tr).children().children()[0]
        this_index = $(this_formGroup).children()[0].value
        $(this_tr).remove()
    })
}
var classList
$('#classManageShow').on('click',function (){
    majorlist
    console.log(majorlist)
    $('#classBody').empty()
    axios.get('/api/admin/class2').then(
        function(res){
            classList = res.data
            console.log(classList)
            for(let i =0;i<classList.length;i++){
                let opens=''
                opens+= `<option ></option>`
                for (let k in majorlist){
                    opens+= `
                    <option value='${majorlist[k]}'
                    ${classList[i].subject==majorlist[k]?'selected':''}>${majorlist[k]}</option>
                    `
                }
                console.log(opens)

                $('#classBody').append( `
              <tr>
                <td>
                  <div class="form-group">
                    <input type="email" class="form-control classID" value="${classList[i].id}" readonly hidden aria-describedby="emailHelp">
                  </div>
                </td>
                <td>
                  <div class="form-group">
                    <input type="email" class="form-control className" value="${classList[i].name}" aria-describedby="emailHelp">
                  </div>
                </td>
                <td>
                  <div class="form-group">
                    <select type="email" class="form-control classDirection" aria-describedby="emailHelp">
                    ${opens}
                    </select>
                  </div>
                </td>
                <td>
                  <div class="form-group">
                    <input type="email" class="form-control classPeopleNum" value="${classList[i].maxcapacity}" aria-describedby="emailHelp">
                  </div>
                </td>
                <td width = '100'>
                  <div class="form-group">
                    <button class="btn btn-dark classDel">删除</button>
                  </div>
                </td>
                </td>
              </tr>
            `)
                addDel()
            }
        }
    )
})







$('#addClass').on('click',function(){
    majorlist
    console.log(majorlist)

    let opens=''
    opens+= `<option ></option>`
    for (let k in majorlist){
        opens+= `<option value='${majorlist[k]}'>${majorlist[k]}</option>`
    }

    $('#classBody').append( `
      <tr>
              <td>
                <div class="form-group">
                  <input type="email" class="form-control classID"  readonly hidden aria-describedby="emailHelp">
                </div>
              </td>
              <td>
                <div class="form-group">
                  <input type="email" class="form-control className" aria-describedby="emailHelp">
                </div>
              </td>
              <td>
              <div class="form-group">
                  <select type="email" class="form-control classDirection" aria-describedby="emailHelp">
                    ${opens}
                  </select>
                </div>
              </td>
              <td>
                <div class="form-group">
                  <input type="email" class="form-control classPeopleNum" aria-describedby="emailHelp">
                </div>
              </td>
              <td width = '100'>
                <div class="form-group">
                  <button class="btn btn-dark classDel">删除</button>
                </div>
              </td>
              </td>
            </tr>
      `)

    addDel()

})


 $('#loadActivityTime').click(function (){
    //受框架影响 2021 5 11 后弃用 无法获取上次 填报的时间
     let url ='api/admin/getActivityTime'//指定url
 //    异步请求 start
     axios.get(url).then(res=>{
         //异步请求end
         res=res.data;
         console.log(res)
         //成功
         if (res.error_code==0){
             //数据装填 start
             console.log(res.data.starttime)
             let start=getFormat(new Date(res.data.starttime*1000));
             let end=getFormat(new Date(res.data.endtime*1000));
             $('#chengTimeModal .startTime').val(start)
             $('#chengTimeModal .endTime').val(end)
             //数据装填 end

             return
         }
         //失败

     })

 })

//修改填报时间
$('#submitTime').on('click',function (){
    //从dome 中 start
    let startTime=$('#chengTimeModal .startTime').val()
    let endTime=$('#chengTimeModal .endTime').val()
    //从dome 中 end
    //数据前端校验 start
    if (!startTime || !endTime){
        alert('开始与结束为必填')
        return
    }
    //日期转时间 start
    startTime=new Date(startTime).getTime()/1000//转换成秒
    endTime=new Date(endTime).getTime()/1000//转换成秒
    //日期转时间 end
    if (startTime==endTime){
        alert('开始与结束时不能一致')
        return;
    }
    if (endTime<startTime){
        alert('结束时间不能小于开始时间')
        return;
    }
    //数据前端校验 end
    let formdata = new FormData(); //初始化容器
    //数据填充 start
    formdata.set('enctype', 'multipart/form-data');
    formdata.set('starttime', startTime);
    formdata.set('endtime', endTime);
    //数据填充 end
    let url='api/admin/changeActivityTime'    //url指定
    //异步请求start
    axios.post(url,formdata).then(res=>{

        //异步请求end
    //    解析数据 start
        res=res.data;
        if (res.error_code==0){
            alert('修改成功')
            $('#chengTimeModal').modal('hide')
            return
        }
        //失败
        //
        alert('修改失败')
    })






})

//班级管理 保存
$('#saveClass').on('click',function(){
    console.log('-0')
    class_tr = $('#classBody').children()
    classField = ['id','name','subject','maxcapacity']
    list = classList
    newClassList=[]
    addClass = []
    delClass =[]
    updataClass =[]
    for(let i =0 ;i < class_tr.length;i++){
        class_td = $(class_tr[i]).children()
        classItem = {}
        for(let j = 0;j < classField.length;j++){
            value = $(class_td[j]).children().children()[0].value
            classItem[classField[j]] = parseInt(value)?parseInt(value):value
        }
        if(classItem.id == ''){
            addClass.push(classItem)
        }else if(classItem.id !== ''){
            newClassList.push(classItem)
        }
    }

    // 寻找被删除的班级记录
    if(newClassList.length !== list.length){
        newId =[]
        oldId =[]


        for(value of newClassList){
            newId.push(value.id)
        }

        for(value of list){
            oldId.push(value.id)
        }

        if(newId.length == 0 ){
            delClass = oldId
        }else{
            for(item of newId){
                if (oldId.indexOf(item) != -1){
                    oldId.splice(oldId.indexOf(item),1)
                }
            }
            delClass = oldId
        }

        // 将已被删除的id剔除出list
        list.filter(function(value,index){
            for(let i = 0 ;i <delClass.length ;i++){
                if(value.id == delClass[i]){
                    list.splice(index,1)
                }
            }
        })

    }

    //将剩下的记录和数据对比 找出更新的数据
    for(item of newClassList){
        for(let i = 0;i < classList.length ;i++){
            if(item.id == classList[i].id){
                if(item.name !== classList[i].name || item.subject !== classList[i].subject ||item.maxcapacity !== classList[i].maxcapacity ){
                    updataClass.push(item)
                }
            }
        }
    }

    if(addClass.length != 0 ){
        // http =
        for(item of addClass){
            axios.post('/api/admin/class2',{operating:1,class:{name:item.name,subject:item.subject,maxcapacity:item.maxcapacity}})
                .then(function(res){
                    if(res.data.message){
                    }
                })
        }
    }

    if(delClass.length != 0 ){
        for(item of delClass){
            axios.post('/api/admin/class2',{operating:-1,id:item})
                .then(function(res){
                    console.log(res.data)
                    if(res.data.message){
                    }
                })
        }
    }

    if(updataClass.length != 0){
        for(item of updataClass){
            axios.post('/api/admin/class2',{operating:0,id:item.id,class:{name:item.name,subject:item.subject,maxcapacity:item.maxcapacity}})
                .then(function(res){
                    console.log(res.data)
                    if(res.data.message){
                    }
                })
        }
    }


    $('#classManage').modal('hide')

})

function getFormat(nTime){
    format = "";
    format += nTime.getFullYear()+"-";
    format += (nTime.getMonth()+1)<10?"0"+(nTime.getMonth()+1):(nTime.getMonth()+1);
    format += "-";
    format += nTime.getDate()<10?"0"+(nTime.getDate()):(nTime.getDate());
    format += "T";
    format += nTime.getHours()<10?"0"+(nTime.getHours()):(nTime.getHours());
    format += ":";
    format += nTime.getMinutes()<10?"0"+(nTime.getMinutes()):(nTime.getMinutes());
    format += ":00";
    return format;
}
